package com.yc.controllers.websocket;


import jakarta.websocket.*;
import jakarta.websocket.server.ServerEndpoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Component
@ServerEndpoint("/email")
@Slf4j
public class EmailWebsocket {
    private static Session session;
 
    @OnOpen
    public void onOpen(Session session) {
//        System.out.println("WebSocket 连接已经建立。");
        log.info("WebSocket 连接已经建立。");
        EmailWebsocket.session = session;
    }
    @OnMessage
    public void onMessage(String message, Session session) throws IOException {
        log.info("收到客户端消息：" + message);
    }
 
    @OnClose
    public void onClose() {
        log.info("WebSocket 连接已经关闭。");
    }
 
    @OnError
    public void onError(Throwable t) {
        log.error("WebSocket 连接出现错误：" + t.getMessage());
    }

    public void send(String message) throws IOException {
        session.getBasicRemote().sendText(message);
    }

}